package ideamaps.settings;


import org.apache.struts2.ServletActionContext;

import com.taillive.core.web.StrutsEntityAction;


public class CustlistAction  extends StrutsEntityAction<Custlist,CustlistManager>{
	

	private static final long serialVersionUID = 7435028121768766819L;
	
	private CustlistManager custlistManager;
	public void setCustlistManager(CustlistManager custlistManager) {
		this.custlistManager = custlistManager;
	}
	
	
	private int itemsSize;
	public int getItemsSize() {
		return itemsSize;
	}
	public void setItemsSize(int itemsSize) {
		this.itemsSize = itemsSize;
	}
	
	
	private Integer itemId;
	public Integer getItemId() {
		return itemId;
	}
	public void setItemId(Integer itemId) {
		this.itemId = itemId;
	}
	

	public String deleteItem() throws Exception {
		CustlistItem custlistItem = custlistManager.get(CustlistItem.class, itemId);
		Custlist custlist = custlistItem.getCustlist();
		custlistManager.removeCustlistItem(custlistItem);
		CustlistCache.updateCache(custlist, ServletActionContext.getServletContext());
		return execute();
	}
	
	@Override
	public String save() {
		custlistManager.saveCustlist(getItem());
		CustlistCache.updateCache(getItem(), ServletActionContext.getServletContext());
		
		return SUCCESS;
	}


	
	@Override
	protected void afterEdit() {
		Custlist custlist = getItem();
		if(custlist.getId()!=null){
			itemsSize = custlist.getItems().size();
		}
	}

}
